package cn.pconline.autoclub.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.service.IntfService;
import cn.pconline.autoclub.service.UserService;
import cn.pconline.passport2.Account;
import cn.pconline.passport2.Passport;
import cn.pconline.passport2.Session;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author wanganning
 */
public class UserAuth {
    private static final Logger log = LoggerFactory.getLogger(UserAuth.class);
    
    @Autowired
    private UserService userService;
    @Autowired
    private Passport passport;
    @Autowired
    private IntfService intfService;
    private User user;

    public User getUser(HttpServletRequest request, HttpServletResponse response) {
//        user = (User) request.getAttribute("user");
    	user = userService.findUser(24998081);
        if (user == null) {
            Session session = passport.recognize(request, response);
            log.info("get account session from passport.");
            if (session != null) {
                Date expiryAt = session.getExpiryAt();
                if(expiryAt.before(new Date())) {
                    log.info("{} get session is out of date: {}", request.getRequestURI(), JSON.toJSONString(session));
                    log.info("{} request info "
                            + "common_session_id param:{}, userId param: {}, "
                            + "cookie:{}, User-Agent: {}",
                            request.getRequestURI(),
                            request.getParameter("common_session_id"),
                            request.getParameter("userId"),
                            request.getHeader("Cookie"), 
                            request.getHeader("User-Agent"));
                }
                user = authUser(session.getAccountId());
            } else {
                user = User.ANONYMOUS;
                log.info("{} get account session from passport is null."
                        + "common_session_id param:{}, userId param: {}, "
                        + "cookie:{}, User-Agent: {}",
                        request.getRequestURI(),
                        request.getParameter("common_session_id"),
                        request.getParameter("userId"),
                        request.getHeader("Cookie"), 
                        request.getHeader("User-Agent"));
            }
            //没有同步过绑定手机号码同步一次
        }else if(StringUtils.isBlank(user.getBindPhone()) && 
            user.getIsSyncdPhone()!= User.IS_SYNC_PHONE && user.getUid()>0){
            Account account = passport.getAccount(user.getUid());
            if(account != null){
                if(account.isActivatedMobile()){
                    user.setBindPhone(account.getMobile());
                }
                user.setIsSyncdPhone(User.IS_SYNC_PHONE);
                userService.updateUser(user);
            }
        }
        return user;
    }

    public User authUser(long userId) {
        User nUser = userService.authUser(userId);
        return nUser;
    }

    public HttpServletRequest setUser(HttpServletRequest request, HttpServletResponse response) {
        user = getUser(request, response);
        request.setAttribute("user", user);
        return request;
    }
}
